<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div style="padding: 5px;">
	<form class="form-horizontal" role="form">
		<div class="form-group">
			<div class="row">
				<label for="serialNumber" class="col-sm-1 control-label" style="width:12%;">机器序列号:</label>
				<div class="col-sm-2">
					<input type="text" class="form-control" id="serialNumber" name="serialNumber">
				</div>
				<label for="orgName" class="col-sm-1 control-label" style="width:12%;">设备所属机构:</label>
				<div class="col-sm-2">
					<input type="text" class="form-control" id="orgName" name="orgName">
				</div>
				<label for="status" class="col-sm-1 control-label" style="width:10%!important;">工单状态:</label>
				<div class="col-sm-1">
					<select id="status" value="${status}">
						<option value="">所有</option>
						<option value="0">未派发</option>
						<option value="1">已派发</option>
						<option value="2">已确认</option>
						<option value="3">维修中</option>
						<option value="4">已完成</option>
						<option value="5">已评价</option>
						<option value="6">反馈</option>
						<!--<option value="7">已删除</option> -->
					</select>
				</div>
				<div class="col-sm-2">
					<button type="button" class="btn btn-primary no-border btn-sm" id="searchBtn">
						<span class="fa fa-search"></span>查询
					</button>
				</div>
			</div>
		</div>
	</form>
	<div id="repairGrid" class="repairGrid"></div>
</div>
<script>
	$(function() {
		var cols = [{
			title : '工单号',
			name : 'id',
			width : '120px'
		},{
			title : '机器序列号',
			name : 'serialNumber',
			width : '180px'
		}, {
			title : '工单状态',
			name : 'statusStr',
			width : '150px',
			render: function(item, name, index){
				if(item.status == 0){
					return '未派发<a href="#" onclick="manualSend('+item.id+',this)">(派单)</a>'
				}else{
					return item[name];
				}
			}
		}, {
            title : '故障代码',
            name : 'breakCode',
            width : '150px'
        }, {
			title : '故障信息',
			name : 'faultInfo',
			width : '250px'
		} , {
			title : '设备地址',
			name : 'machineAddress',
			width : '200px'
		}, {
			title : '设备所属机构',
			name : 'orgName',
			width : '150px'
		}, {
			title : '报修人',
			name : 'bankUser',
			width : '150px'
		}, {
			title : '报修人手机号码',
			name : 'bankUserPhone',
			width : '150px'
		}, {
			title : '报修人单位',
			name : 'bankUserCompany',
			width : '180px'
		}, {
			title : '维修人',
			name : 'maintenanceUser',
			width : '150px'
		},{
			title : '维修人手机号码',
			name : 'maintenancePhone',
			width : '150px'
		}, {
			title : '联系人',
			name : 'contact',
			width : '150px'
		},{
			title : '联系人手机号码',
			name : 'contactPhone',
			width : '150px'
		},{
			title : '期望维修时间',
			name : 'expectedRepairTimeStr',
			width : '150px'
		},{
			title : '预计维修时间',
			name : 'planRepairTimeStr',
			width : '150px'
		},{
			title : '报修日期',
			name : 'repairDateStr',
			width : '150px'
		}, {
			title : '维护开始时间',
			name : 'maintenanceTimeStartStr',
			width : '150px'
		}, {
			title : '完成时间',
			name : 'maintenanceTimeEndStr',
			width : '150px'
		}, {
			title : '创建时间',
			name : 'createTimeStr',
			width : '150px'
		}, {
			title : '备注',
			name : 'maintenanceRemark',
			width : '150px'
		}];
		var buttons = [{
			content : '<button class="btn btn-info no-border btn-sm" type="button"><span class="fa fa-search"></span>查看</button>',
			action : 'view'
		}, {
			content : '<button class="btn btn-primary no-border btn-sm" type="button"><span class="fa fa-plus"></span>&nbsp;新增</button>',
			action : 'add'
		}, {
			content : '<button class="btn btn-success no-border btn-sm" type="button"><span class="fa fa-pencil-square-o"></span>&nbsp;修改</button>',
			action : 'modify'
		}, {
			content : '<button class="btn btn-danger no-border btn-sm" type="button"><span class="fa fa-trash-o"></span>&nbsp;删除</button>',
			action : 'delete'
		}, {
			content : '<button class="btn btn-default no-border btn-sm" type="button"><span class="fa fa-download"></span>&nbsp;导出</button>',
			action : 'export'
		}];
		var url = contextPath + '/repair/query';
		var status = '${status}';
		var searchCondition = {};
		if(status != null){
			searchCondition.status = status;
			$('#status').val(status);
		}
		$('#repairGrid').grid({
			identity : 'id',
			columns : cols,
			buttons : buttons,
			searchCondition: searchCondition,
			url : contextPath + '/repair/query'
		}).on({
			'view' : function(event, data) {
				var indexs = data.data;
				var $this = $(this);
				if (indexs.length == 0) {
					$this.message({
						type : 'warning',
						content : '请选择一条记录进行操作'
					})
					return;
				}
				if (indexs.length > 1) {
					$this.message({
						type : 'warning',
						content : '只能选择一条记录进行操作'
					})
					return;
				}
				seajs.use('repair', function(repair) {
					repair.view(indexs[0]);
				});
			},
			'add' : function() {
				seajs.use('repair', function(repair) {
					repair.add();
				});
			},
			'modify' : function(event, data) {
				var indexs = data.data;
				var $this = $(this);
				if (indexs.length == 0) {
					$this.message({
						type : 'warning',
						content : '请选择一条记录进行修改'
					})
					return;
				}
				if (indexs.length > 1) {
					$this.message({
						type : 'warning',
						content : '只能选择一条记录进行修改'
					})
					return;
				}
				seajs.use('repair', function(repair) {
					repair.modify(indexs[0], $this);
				});
			},
			'delete' : function(event, data) {
				var indexs = data.data;
				var $this = $(this)
				if (indexs.length == 0) {
					$this.message({
						type : 'warning',
						content : '请选择要删除的记录'
					})
					return;
				}
				$this.confirm({
					content : '确定要删除所选记录吗?',
					callBack : function() {
						seajs.use('repair', function(repair) {
							repair.remove(indexs, $this);
						});
					}
				});
			},
			'export': function(){
				var searchCondition = $(this).getGrid().searchCondition;
				var data = [];
				for(var prop in searchCondition){
					if(searchCondition[prop] && searchCondition[prop].length > 0){
						data.push(prop + '=' + searchCondition[prop]);
					}
				}
				window.open(contextPath + '/repair/exportExcel'+ (data.length == 0 ? '' : '?'+data.join('&')), "导出工单");
			}
		});
		$('#searchBtn').on('click', function(){
			seajs.use('util', function(util){
				$('#repairGrid').getGrid().search(util.getFormData(pageContents.find('form')));		
			});
		});
	}); 
	function manualSend(id, obj){
		$('#repairGrid').loader({
			title: '正在派发...'
		});
		$.get(contextPath+'/repair/manualSend/'+id).done(function(data){
			$('#repairGrid').loader('hide');
			if (data.statusCode == 20011011) {
					$('body').message({
						type : 'success',
						content : '派单成功'
					});
					$(obj).parent().html('已派发');
				} else {
					$('body').message({
						type : 'error',
						content : data.msg
					});
				}
		});
	}
</script>
